﻿using DAL.Repositories;
using EntitiesLayer;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Drawing;
using System.IO;
using DAL.AbstractRepositories;

namespace DAL.Repositorios
{
    public class MaquinaRepository : DAL.AbstractRepositories.AbstractRepository<Maquina>

    {
  
        public override void Insert(Maquina entity)
        {
            Inserted.Add(entity);
        }

        public override void Delete(Maquina entity)
        {
            Deleted.Add(entity);
        }

        public override void Update(Maquina entity)
        {
            Updated.Add(entity);
        }
       
        public override IEnumerable<Maquina> GetAll()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarMaquinas");
            ds.Tables[0].Columns.Add(new DataColumn("ImagenBase64", typeof(string)));

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dr["ImagenBase64"] = Convert.ToBase64String((byte[])dr["FotoMaquina"]);
                yield return crearMaquina(dr, Convert.ToInt32(dr["IdMaquina"])); 
            }

        }

        public DataSet GetTipoMaquinas()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarTipoMaquina");
            return ds;
        }

        public DataSet GetListaMaquinas()
        {
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaListarMaquinas");
            return ds;
        }

        public int GetIdTipoMaquinaByDesc(string descripcion)
        {
            int id = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@DescripcionTipo", descripcion));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaGetIdByTipoMaquina");

            foreach (DataRow dr in ds.Tables[0].Rows)
                id = Convert.ToInt32(dr["idTipoMaquina"]);

            return id;
        }

        public override  Maquina GetById(int id)
        {
            Maquina objMaquina = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdMaquina", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarMaquinaPorID");
            ds.Tables[0].Columns.Add(new DataColumn("ImagenBase64", typeof(string)));

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dr["ImagenBase64"] = Convert.ToBase64String((byte[])dr["FotoMaquina"]);
                objMaquina = crearMaquina(dr, id);
            }

            return objMaquina;

        }


        //string nombreMaquina, string serieMaquina, int numeroMaquina, char estado, Bitmap imagen, string indicacionesUso, string tipoMaquina, int id = 0
        internal static Maquina crearMaquina(DataRow dr, int id)
        {
            object foto = dr["FotoMaquina"];
            Maquina objMaquina = new Maquina(
                      nombreMaquina: dr["NombreMaquina"].ToString(),
                      serieMaquina: dr["NumeroSerie"].ToString(),
                      numeroMaquina: Convert.ToInt32( dr["NumeroMaquina"]),
                      estado: Convert.ToChar(dr["Estado"]),
                      imagen: foto!= DBNull.Value ? ((byte[])dr["FotoMaquina"]).toBitMap():null,
                      indicacionesUso: dr["IndicacionesUso"].ToString(),
                      idTipoMaquina: Convert.ToInt32(dr["IdTipoMaquina"]),
                      id: id,
                      imagenBase64 : dr["ImagenBase64"].ToString()
                  );

            return objMaquina;
        }

        protected override void InsertEntity(Maquina objMaquina)
        {
            try
            {
                //ImageConverter convertidor = new ImageConverter();
                //byte[] imagen = (byte[])convertidor.ConvertTo(objMaquina.Imagen, typeof(byte[]));

                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@NombreMaquina", objMaquina.NombreMaquina));
                cmd.Parameters.Add(new SqlParameter("@NumeroSerie", objMaquina.NumeroSerie));
                cmd.Parameters.Add(new SqlParameter("@NumeroMaquina", objMaquina.NumeroMaquina));
                cmd.Parameters.Add(new SqlParameter("@Estado", objMaquina.Estado));
                cmd.Parameters.Add(new SqlParameter("@Imagen", EntitiesLayer.NuestrasExtenciones.ToByte(objMaquina.Imagen)));
                cmd.Parameters.Add(new SqlParameter("@IndicacionesUso", objMaquina.IndicacionesUso));
                cmd.Parameters.Add(new SqlParameter("@TipoMaquina", objMaquina.IdTipoMaquina));
                DBAccess.ExecuteSPNonQuery(ref cmd, "PaRegistrarMaquina");
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

        }

        protected override void UpdateEntity(Maquina objMaquina)
        {
              //ImageConverter convertidor = new ImageConverter();
                //byte[] imagen = (byte[])convertidor.ConvertTo(objMaquina.Imagen, typeof(byte[]));
                
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@IdMaquina", objMaquina.Id));
                cmd.Parameters.Add(new SqlParameter("@NombreMaquina", objMaquina.NombreMaquina));
                cmd.Parameters.Add(new SqlParameter("@NumeroSerie", objMaquina.NumeroSerie));
                cmd.Parameters.Add(new SqlParameter("@NumeroMaquina", objMaquina.NumeroMaquina));
                cmd.Parameters.Add(new SqlParameter("@Estado", objMaquina.Estado));
                cmd.Parameters.Add(new SqlParameter("@Imagen", EntitiesLayer.NuestrasExtenciones.ToByte(objMaquina.Imagen)));
                cmd.Parameters.Add(new SqlParameter("@IndicacionesUso", objMaquina.IndicacionesUso));
                cmd.Parameters.Add(new SqlParameter("@TipoMaquina", objMaquina.IdTipoMaquina));
                DBAccess.ExecuteSPNonQuery(ref cmd, "PaModificarMaquina");
         

        }
        protected override void DeleteEntity(Maquina objMaquina)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdMaquina", objMaquina.Id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaEliminarMaquina");
        }
    }
}
